<?php

/*嘉奖20170709*/

namespace Admin\Model;
use Think\Model;

class HrCommendModel extends Model {

    public function dadd( $data ) {
		$rback = array('rcode'=>0, 'data'=>null, 'msg'=>'null');
        $strid = $data['user_id'];
        if (strlen($strid) < 1) {
            $rback['msg'] = '没有选择工号或姓名';
            return $rback;
        }
        $is_date = strtotime($data['comm_date']) ? true : false;
        if (!$is_date) {
            $rback['msg'] = '日期格式不正确';
            return $rback;
        }
        if (!is_numeric($data['comm_type']) or $data['comm_type'] == '-1') {
            $rback['msg'] = '没有选择类别';
            return $rback;
        }
        if (!is_numeric($data['comm_class']) or $data['comm_class'] == '-1') {
            $rback['msg'] = '没有选择分类';
            return $rback;
        }

        $spuinfo = explode(',', $strid);
        $where = array();
        if (count($spuinfo) > 0) {
            if (strlen($data['comm_reason']) > 50) {
                $data['comm_reason'] = substr($data['comm_reason'], 0, 50);
            }
            if (strlen($data['comm_clsay']) > 50) {
                $data['comm_clsay'] = substr($data['comm_clsay'], 0, 50);
            }
            if (strlen($data['comm_beizhu']) > 200) {
                $data['comm_beizhu'] = substr($data['comm_beizhu'], 0, 200);
            }

        	for ($i=0; $i < count($spuinfo); $i++) { 
        		$uvalue = explode('_', $spuinfo[$i]);
        		if (count($uvalue) == 4) {
        			$data['user_id'] = $uvalue[0];
        			$data['comm_sector'] = $uvalue[1];
        			$data['comm_role'] = $uvalue[2];
        			$data['comm_exstatus'] = $uvalue[3];
        			//where
        			$where['user_id'] = $uvalue[0];
        			$where['comm_type'] = $data['comm_type'];
        			$where['comm_class'] = $data['comm_class'];
        			$where['comm_date'] = $data['comm_date'];
        			$this->where( $where )->find();
        			if (!$plist) {
        				$this->add( $data );
        			}
        			$rback['rcode'] = 1;
        			$rback['msg'] = '添加成功';
        		}
        	}        	
        }
        return $rback;
    }
    //编辑
    public function dsave( $data ){
        $rback = array('rcode'=>0, 'data'=>null, 'msg'=>'error');
        if (!is_numeric($data['comm_id'])) {
            $rback['msg'] = 'id参数错误';
            return $rback;
        }
        $is_date = strtotime($data['comm_date']) ? true : false;
        if (!$is_date) {
            $rback['msg'] = '日期格式不正确';
            return $rback;
        }
        if (!is_numeric($data['comm_type']) or $data['comm_type'] == '-1') {
            $rback['msg'] = '没有选择类别';
            return $rback;
        }
        if (!is_numeric($data['comm_class']) or $data['comm_class'] == '-1') {
            $rback['msg'] = '没有选择分类';
            return $rback;
        }        
        if (strlen($data['comm_reason']) > 50) {
            $data['comm_reason'] = substr($data['comm_reason'], 0, 50);
        }
        if (strlen($data['comm_clsay']) > 50) {
            $data['comm_clsay'] = substr($data['comm_clsay'], 0, 50);
        }
        if (strlen($data['comm_beizhu']) > 200) {
            $data['comm_beizhu'] = substr($data['comm_beizhu'], 0, 200);
        }
        if ($this->save($data)) {
            $rback['rcode'] = 1;
            $rback['msg'] = '编辑成功';
        }
        return $rback;
    }

    //删除
    public function ddelete( $did ){
        $rback = array('rcode'=>0, 'data'=>null, 'msg'=>'');
        if ( $did && is_numeric($did) && $did > 0 ) {
            if ($this->delete( $did )) {
                $rback['rcode'] = 1;
                $rback['msg'] = '删除成功';
            }else{
                $rback['msg'] = '很抱歉，删除成功';
            }
        }else{
            $rback['msg'] = '参数错误';
        }
    	return $rback;	
    }

}